package com.hongdee.crm.wo.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.Template.entity.TCrmTemplateRef;
import com.hongdee.crm.Template.service.ITCrmTemplateService;
import com.hongdee.crm.wo.entity.TCrmWoType;
import com.hongdee.crm.wo.service.ITCrmWoService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by dell on 2017/5/9.
 */
@RestController
@RequestMapping("/crm/wo")
public class TCrmWoController  {
    @Autowired
    private ITCrmWoService itCrmWoService;
    /**
     * 保存工单对应类别接口
     * */
    @RequestMapping("/type/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmWoType tCrmWoType) throws Exception {
        EntityUtils.valid(tCrmWoType);
        TCrmWoType group = itCrmWoService.saveOrUpdate(tCrmWoType,uid);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        System.out.println("这是我返回的东西"+res);
        return res;
    }
    /*
    * 查询类别详情接口
    * */
    @RequestMapping("/type/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        Map map = itCrmWoService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * 停用工单类别对应接口
     */
    @RequestMapping("/type/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmWoService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 启用维系类别接口
     */
    @RequestMapping("/type/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmWoService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }
    /*
    * 分页
    * */
    @RequestMapping("/type/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit
    ) throws Exception {
        /*
        * 获取公司id
        * */
        String oid= TokenUtil.getCompanyId(uid);
        Map map = this.itCrmWoService.newPage(start, limit,oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
}
